<?php require_once('../../../Connections/db_2e3r.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? $theValue : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_db_2e3r, $db_2e3r);
$query_entregas = "SELECT * FROM entregas inner join clientes on cli_id = ent_cli_id";
$entregas = mysql_query($query_entregas, $db_2e3r) or die(mysql_error());
$row_entregas = mysql_fetch_assoc($entregas);
$totalRows_entregas = mysql_num_rows($entregas);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3c.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<link href="../is.css" rel="stylesheet" type="text/css"/>
<title>2E3R - Mapa </title>
</head>
<? $gmaps_key =  strpos($_SERVER['HTTP_REFERER'],'localhost')> 0 ? 'ABQIAAAAB7HQOTzfRkGPelmTKJdqfhSpovsP02133glhVSAL49w0N977JhQ42OiZG5Fu-GJuOSAn0s4gfzu-EA' :
'ABQIAAAA5WhXjdq80_apmvTkyUXc2hR51vzwznkdwk-6WN5HjzzFs-ncXhT1aYBl4OO_qIu4qRVcKGfPtjn5LQ';
?>
<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=<?=$gmaps_key?>" type="text/javascript"></script>

 <?php if ($totalRows_entregas > 0) { // Show if recordset not empty ?>
 <? $i= 0; ?>
<script>
	var endereco = new Array(<?=$totalRows_entregas?>);
  <?php do { ?>

	endereco[<?=$i++?>] = "<?=$row_entregas['ent_endereco']?>";
	   
   <?php } while ($row_entregas = mysql_fetch_assoc($entregas)); ?>
</script>

  <?php } // Show if recordset not empty ?>


<script type="text/javascript">

    var map = null;
    var geocoder = null;

    function initialize() {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_canvas"));
		map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        map.setCenter(new GLatLng(-23.48198365413908, -46.50092124938965), 13);
        geocoder = new GClientGeocoder();
		adressControl();
      }
    }
	
	function adressControl(){
		for(i=0; i<endereco.length; i++){
			showAddress(endereco[i]);
		}
	}

    function showAddress(address) {
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
              map.setCenter(point, 11);
              var marker = new GMarker(point);
              map.addOverlay(marker);
			  GEvent.addListener(marker,"click",function(){
				marker.openInfoWindowHtml(address);
			  });
            }
          }
        );
      }
    }
    </script>
  </head>

  <body onload="initialize()" onunload="GUnload()">

  <p>&nbsp;
  <?   mysql_data_seek($entregas, 0); ?>
    <?php if ($totalRows_entregas > 0) { // Show if recordset not empty ?>
    <?php do { ?>
    <?php echo $row_entregas['ent_endereco']; ?><br />
        <?php } while ($row_entregas = mysql_fetch_assoc($entregas)); ?>
      <?php } // Show if recordset not empty ?>
</p>
  <table align="center" width="728">
	<tr><td height="40"></td></tr>
	<tr>
		<td align="center"><div id="map_canvas" style="width: 600px; height: 500px"></div></td>
	</tr>
	<tr><td height="40" align="rigth" >
		<a href="#" class="azulboldgde">Cadastrar Entregas</a>
	</td></tr>
  </table>
  <center></center>
  </body>
</html>
<?php
mysql_free_result($entregas);
?>